/**
 * 谁是跳王？
 */

import Data from './Data'

 export default class King extends Laya.Script {

    constructor() { super() }

    // 数据
    private _data: any

    private rig: Laya.RigidBody // 刚体
    private sprite: Laya.Sprite // 精灵

    private pa_contrl: boolean = true // 落地声的间隔控制

    onEnable(): void {
        this._data = Data

        this.sprite = this.owner as Laya.Sprite
        // this.sprite.loadImage('images/sb.png')
        this.rig = this.owner.getComponent(Laya.RigidBody) as Laya.RigidBody
        // 初始化跳跃状态
        this.setSprite(this._data._dir, true)
        this.addListener()
    }

    onTriggerEnter(target: any, self: any, content: any): void {
        if (target.label == 'up') {
            Laya.stage.event("jump_end")
            this.setSprite(this._data._dir, false)
            if (this.pa_contrl) {
                Laya.SoundManager.playSound("http://qiniu.wqnmd.cn/jump_king/king_pa.wav")
                this.pa_contrl = false
                setTimeout(() => {
                    this.pa_contrl = true
                }, 500)
            }
            
        }
        if (target.label == 'left') {
            this._data._dir = false
            this.setSprite(this._data._dir, true)
            Laya.SoundManager.playSound("http://qiniu.wqnmd.cn/jump_king/king_boom.wav")
        }
        if (target.label == 'right') {
            this._data._dir = true
            this.setSprite(this._data._dir, true)
            Laya.SoundManager.playSound("http://qiniu.wqnmd.cn/jump_king/king_boom.wav")
        }
    }

    onUpdate(): void {
        if (this.sprite.y < - this.sprite.height) { // 去上一层
            this.sprite.once('up_level', this, this.up_level)
            this.sprite.event('up_level')
        }
        if (this.sprite.y > Laya.stage.height) { // 去下一层
            this.sprite.once('down_level', this, this.down_level)
            this.sprite.event('down_level')
        }
    }

    // 去上一层
    private up_level(): void {
        this._data._x = this.sprite.x
        this._data._y = Laya.stage.height - 1
        this._data._vx = this.rig.linearVelocity.x
        this._data._vy = this.rig.linearVelocity.y
        let str = 'scene/level_'+(this._data._level + 1)+'.scene'
        Laya.Scene.open(str)
        this._data._level += 1
        // 更新历史最高
        if ( this._data._level + 1 > this._data._historyTop ) {
            this._data._historyTop = this._data._level + 1
        }
        Laya.SoundManager.playSound("http://qiniu.wqnmd.cn/jump_king/scene_up.wav")
        this.sprite.removeSelf()
    }

    // 去下一层
    private down_level(): void {
        this._data._x = this.sprite.x
        this._data._y = 1 - this.sprite.height
        this._data._vx = this.rig.linearVelocity.x
        this._data._vy = this.rig.linearVelocity.y
        let str = 'scene/level_'+(this._data._level - 1)+'.scene'
        Laya.Scene.open(str)
        this._data._level -= 1
        Laya.SoundManager.playSound("http://qiniu.wqnmd.cn/jump_king/scene_down.wav")
        this.sprite.removeSelf()
    }

    // 事件监听
    private addListener(): void {
        let vm = this
        // 跳跃
         Laya.stage.on("jumping", this, function (data: any) {
            vm.jumping(data)
        });
    }

    // 跳跃
    private jumping(data: any): void {
        let p = data.power / 1.2
        // 最大跳跃高度 ??
        // 最大跳跃距离 ??
        this.rig.setVelocity({x: data.dir ? p : -p, y: -p * 3})
        this._data._dir = data.dir
        this.setSprite(this._data._dir, true)
        Laya.SoundManager.playSound("http://qiniu.wqnmd.cn/jump_king/king_jump.wav")
    }


    // 设置速度
    public setSpeed(x: number, y: number): void {
        this.rig.setVelocity({x:x, y:y})
    }

    // 设置精灵
    private setSprite(dir: boolean, jump: boolean ): void {
        if (dir && jump) {
            this.sprite.loadImage('images/sbjr.png')
        } else if (dir && !jump) {
            this.sprite.loadImage('images/sbr.png')
        } else if (!dir && jump) {
            this.sprite.loadImage('images/sbj.png')
        } else {
            this.sprite.loadImage('images/sb.png')
        }
    }

 }